
/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-06-24 13:29:03
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-17 10:54:07
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { CodingEditOrAddComponent } from './coding-edit-or-add/coding-edit-or-add.component';
//查看履历
import { ShowResumeComponent } from './show-resume/show-resume.component';
import { CodeFormatDescServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
@Component({
    selector: 'app-codind-management',
    templateUrl: './codind-management.component.html',
    styleUrls: ['./codind-management.component.less']
})
export class CodindManagementComponent extends AppComponentBase {
    @ViewChild('codingEditOrAdd') codingEditOrAdd: CodingEditOrAddComponent
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('showResume') ShowResume: ShowResumeComponent;
    codeName: string = '';
    isUsed: any;
    functionModule: any;
    selectRow: any;// 选中的行数据
    title: string = '';
    rowdata: any;
    functionOptions = [
        {
            label: '部门管理',
            value: '部门管理'
        }, {
            label: '物料建模',
            value: '物料建模'
        }, {
            label: '业务划分',
            value: '业务划分'
        }, {
            label: '合同管理',
            value: '合同管理'
        }, {
            label: '订单管理',
            value: '订单管理'
        }
    ];
    isUsedOptions = [
        {
            label: '启动',
            value: 1
        }, {
            label: '停用',
            value: 0
        }
    ];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _codeFormatDescServiceProxy: CodeFormatDescServiceProxy,
        private _messageService: MessageService,
    ) {
        super(injector);
    }

    ngOnInit() {
    }
    getTableList(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }
        this._codeFormatDescServiceProxy.getCodeFormatList(
            this.codeName,
            this.isUsed,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    //重置
    reset() {
        this.isUsed = undefined;
        this.functionModule = undefined;
        this.codeName = '';
        this.selectRow = undefined;
        this.getTableList()
    }
    //新增
    addOrUpdate(row?: any) {
        if (row != undefined) {
            this.rowdata = row;
            this.title = '修改编码规则'
        } else {
            this.title = '新增编码规则';
        }
        this.codingEditOrAdd.show();
    }
    //启用停用
    enable(rs) {
        if (this.selectRow == undefined || Object.keys(this.selectRow).length == 0) {
            this._messageService.error('未选择数据！');
            return;
        }
        if (rs) {
            let used = 1;
            //启用
            this._codeFormatDescServiceProxy.enableOrDisableUpdate(
                this.selectRow,
                used
            ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this._messageService.success('启用成功！');
                this.reset();
                this.getTableList();
            });
        } else {
            //停用
            let used = 0;
            this._codeFormatDescServiceProxy.enableOrDisableUpdate(
                this.selectRow,
                used
            ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this._messageService.success('停用成功！');
                this.reset();
                this.getTableList();
            });
        }
    }
    delete() {
        this.message.confirm(
            '确认删除这条数据吗',
            (isConfirmed) => {
                if (isConfirmed) {
                    if (this.saveFlag) {
                        this.saveFlag = false;
                        //删除
                        this._codeFormatDescServiceProxy.deleteCodeFormat(
                            this.selectRow
                        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                            this.saveFlag = true;
                            if (result.code == 0) {
                                this._messageService.success('删除成功！');
                                this.reset();
                                this.getTableList();
                            } else {
                                this._messageService.error(result.message);
                            }
                        });
                    }
                }
            }
        );

    }
    look(row) {
        //履历
        this.rowdata = row;
        setTimeout(() => {
            this.ShowResume.show()
            this.ShowResume.getTableList()
        }, 20)
    }
}
